import store from '../store/store.js';
import * as ActionTypes from '../constants/constants.js';
import { Actions } from 'react-native-router-flux';
import { path, saveData, loadData, removeData, requstPromise } from '../util/utils.js';
import request from '../api/request.js';



// antd 按需加载
import { Toast, Portal } from '@ant-design/react-native';

// 登录
export async function login(option) {
  const { dispatch } = store
  let loading = Toast.loading('登录中', 0);
  const res = await request.login(option)
  requstPromise(res).then((res) => {
    dispatch({
      type: ActionTypes.setUserInfo,
      data: res.data
    })
    Portal.remove(loading)
    Toast.info('登录成功', 1);
    Actions.exchange();
  }).catch((error) => {
    Portal.remove(loading)
    Toast.info(error.message);
  })
}

// 获取验证码
export async function getCode(option) {
  const { dispatch } = store
  const res = await request.getCode(option)
  return requstPromise(res)
}

// 获取首页的数据
export async function index(option) {
  const { dispatch } = store
  const res = await request.index(option)
  return requstPromise(res)
}

// 合约
export async function recommend(option) {
  const { dispatch } = store
  const res = await request.recommend(option)
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  })
}

// 交易所列表
export async function ex(option) {
  const { dispatch } = store
  const res = await request.ex(option)
  return requstPromise(res)
}

// 查询当前持仓或历史持仓
export async function positionHistory(option) {
  const { dispatch } = store
  const res = await request.positionHistory(option)
  return requstPromise(res)
}

// 查询当前持仓或历史持仓
export async function positionDetail(option) {
  const { dispatch } = store
  const res = await request.positionDetail(option)
  return requstPromise(res)
}

// 订阅实盘
export async function subscription(option) {
  const { dispatch } = store
  const res = await request.subscription(option)
  return requstPromise(res)
}

// 取消订阅实盘
export async function unsubscription(option) {
  const { dispatch } = store
  const res = await request.unsubscription(option)
  return requstPromise(res)
}

// 订阅实盘列表
export async function subscriptionList(option) {
  const { dispatch } = store
  const res = await request.subscriptionList(option)
  return requstPromise(res)
}

// 订阅实盘列表
export async function access(option) {
  const { dispatch } = store
  const res = await request.access(option)
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  })
}

// 订阅实盘列表
export async function modify(option) {
  const { dispatch } = store
  const res = await request.modify(option)
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  })
}

// 查询用户已介入实盘
export async function exById(option) {
  const { dispatch } = store
  const res = await request.exById(option)
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  })
}

// 获取用户信息
export async function getUser(option) {
  const { dispatch, getState } = store
  const res = await request.getUser({
    userId: getState().userInfo.id
  })
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  }).then(user => {
    dispatch({
      type: ActionTypes.setUserInfo,
      data: user.data
    })
    console.log(user)
  })
}

// 上传
export async function upload(option) {
  const { dispatch } = store
  console.log(option)
  const res = await request.upload(option)
  return requstPromise(res).catch(error => {
    console.log(error)
    Toast.info(error.message)
  })
}
